package com.example.demo.mapper;

import com.example.demo.entity.Teacher;
import com.example.demo.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface TeacherMapper {
    // 新增教师
    @Insert("INSERT INTO teachers (user_id, real_name, introduction, avatar) VALUES (#{user_id}, #{real_name}, #{introduction}, #{avatar})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertTeacher(Teacher teacher);

    // 删除教师
    @Delete("DELETE FROM teachers WHERE id = #{id}")
    int deleteTeacherById(int id);

    // 修改教师
    @Update("UPDATE teachers SET user_id = #{user_id}, real_name = #{real_name}, introduction = #{introduction}, avatar = #{avatar} WHERE id = #{id}")
    int updateTeacher(Teacher teacher);

    // 查询教师
    @Select("SELECT * FROM teachers WHERE id = #{id}")
    Teacher selectTeacherById(int id);

    // 查询所有教师
    @Select("SELECT * FROM teachers")
    List<Teacher> selectAllTeachers();


    // 与 users 表联合查询教师及其用户信息
//    @Select("SELECT t.*, u.username, u.email, u.role, u.created_at FROM teachers t " +
//            "JOIN users u ON t.user_id = u.id WHERE t.id = #{teacherId}")
//    @Results({
//            @Result(property = "id", column = "id"),
//            @Result(property = "user_id", column = "user_id"),
//            @Result(property = "real_name", column = "real_name"),
//            @Result(property = "introduction", column = "introduction"),
//            @Result(property = "avatar", column = "avatar"),
//            @Result(property = "user", javaType = User.class,
//                    one = @One(select = "com.example.demo.mapper.UserMapper.selectUserById"))
//    })
//    Teacher selectTeacherWithUser(int teacherId);
}
